window.shootPeople = window.shootPeople || {};
shootPeople.Game = function () {
    var s = this;
    annie.Sprite.call(s);
    /*_a2x_need_start*/s.b1=null;s.eme=null;s.goldcoin=null;s.life_mc=null;s.people=null;s.skill1=null;s.skill2=null;s.skill3=null;/*_a2x_need_end*/
    annie.initRes(s, "shootPeople", "Game");
    s.skill1.mouseChildren = false;
    s.skill2.mouseChildren = false;
    s.skill3.mouseChildren = false;
    s.enemyList = [];       //恶魔数组
    s.arrowList = [];       //弓箭数组
    s.arrowType = 0;        //箭初始技能状态
    s.isStart = true;       //游戏开关
    s.timeCount = 0;        //恶魔出现频率
    s.arrowTime = 0;        //弓箭频率
    s.gongRotation = 0;     //弓箭角度
    s.monsterTime = 0;      //怪兽出现频率
    s.skillCoolingArr = [1, 2, 2]; //技能冷却
    s.skillCoolingTime1 = 0;
    s.skillCoolingTime2 = 5;
    s.skillCoolingTime3 = 0;
    s.skillStatus1 = false;
    s.skillStatus2 = false;
    s.skillStatus3 = false;
    s.arrowAngleArr = [0, 5, 10, 15, -5, -10, -15];
    s.sheSound = new annie.Sound("./resource/she.wav");
    s.billionSheSound = new annie.Sound("./resource/doushe.wav");
    s.hitSound = new annie.Sound("./resource/hit.wav");
    s.skill1Sound = new annie.Sound("./resource/skill1.wav");
    s.skill3Sound = new annie.Sound("./resource/skill3.wav");
    s.count = 0;
    s.rotaionFalse = false;
    s.zzl = 0;
    var pointRadian = 0,    //指向弧度
        life = 1,//生命
        stageWdith = 0,
        stageAllWidth = 0;
    s.addEventListener(annie.Event.ADD_TO_STAGE, function (e) {
        stageWdith = s.stage.viewRect.x;
        stageAllWidth = s.stage.viewRect.width;
        s.skill1.cooling_txt.visible = false;
        s.skill2.cooling_txt.visible = false;
        s.skill3.cooling_txt.visible = false;
        s.isStart = true;
        s.people.gong.rotation = 130;
    });

    s.addEventListener(annie.MouseEvent.MOUSE_UP, function (e) {
        s.gongRotationHandler(e.stageY, e.stageX);
        if (e.target.name == "skill1" || e.target.name == "skill2" || e.target.name == "skill3") {
            s.ArrowSkill(e.target.name);
        } else {
            // s.launchArrow();
        }
    });
    var maxDis = 1000;
    s.addEventListener(annie.Event.ENTER_FRAME, function (e) {
        if (s.isStart) {
            ////enemy
            var enemy = null;
            for (var i = s.enemyList.length - 1; i >= 0; i--) {//生成小恶魔
                enemy = s.enemyList[i];
                if (enemy.motion == "x") {
                    enemy.x -= enemy.speed;
                    enemy.scaleX = 1;
                } else {
                    enemy.scaleX = -1;
                    enemy.x += enemy.speed;
                }
                if (enemy.x < -100 || enemy.x > 800) {
                    if (enemy.name == "devil") {
                        shootPeople.DevilManager.recoveryEnemy(enemy);
                        life++;
                        s.life_mc.gotoAndStop(life);
                        if (life > 3) {
                            s.gameOver()
                        }
                    } else if (enemy.name == "monster") {
                        shootPeople.MonsterManager.recoveryEnemy(enemy);
                    }
                    s.enemyList.splice(i, 1);
                    s.removeChild(enemy);
                }
            }
            var arrow = null;
            for (var i = s.arrowList.length - 1; i >= 0; i--) {//生成箭矢
                arrow = s.arrowList[i];
                arrow.y = arrow.startY + Math.sin(arrow.direction) * arrow.dis;
                arrow.x = arrow.startX + Math.cos(arrow.direction) * arrow.dis;
                arrow.dis += arrow.speed;
                if (arrow.type == 4) {
                    maxDis = 200;
                } else {
                    maxDis = 800;
                }
                if (arrow.dis > maxDis) {
                    shootPeople.ArrowManager.recoveryBullet(arrow);
                    s.arrowList.splice(i, 1);
                    s.removeChild(arrow);
                }
            }
            ///hit
            var isHit = false;
            var cArrowList = [];
            for (var i = s.enemyList.length - 1; i >= 0; i--) {
                enemy = s.enemyList[i];
                isHit = false;
                for (var j = s.arrowList.length - 1; j >= 0; j--) {
                    arrow = s.arrowList[j];
                    if (annie.Point.distance(enemy.x, enemy.y, arrow.x, arrow.y) < 40) { //碰撞检测
                        s.hitSound.play();
                        if (enemy.scaleX == -1) {
                            enemy.gotoAndStop(3);
                            enemy.b.play();
                        } else {
                            enemy.gotoAndStop(2);
                            enemy.m.play();
                        }
                        if (arrow.type != 1) {
                            if (arrow.type == 3) {
                                s.skill3Sound.play();
                                for (var m = 0; m < 6; m++) {
                                    var bullet = shootPeople.ArrowManager.getArrow();
                                    bullet.direction = m * Math.PI / 3;
                                    bullet.x = arrow.x;
                                    bullet.y = arrow.y;
                                    bullet.startX = arrow.x;
                                    bullet.startY = arrow.y;
                                    bullet.rotation = m * 60;
                                    bullet.speed = 20;
                                    bullet.dis = 0;
                                    bullet.type = 4;
                                    bullet.gotoAndStop(3);
                                    s.addChild(bullet);
                                    cArrowList.push(bullet);
                                    s.people.gong.gotoAndPlay(2);
                                }
                            }
                            shootPeople.ArrowManager.recoveryBullet(arrow);
                            s.arrowList.splice(j, 1);
                            s.removeChild(arrow);
                        }
                        isHit = true;
                        //hit success
                        break;
                    }
                }
                if (isHit) {
                    var emee = s.enemyList.splice(i, 1);

                    if (emee[0].name == "devil") {
                        s.count += 5;
                        if (emee[0].scaleX == 1) {
                            shootPeople.DevilManager.recoveryEnemy(emee[0], "m");
                        } else {
                            shootPeople.DevilManager.recoveryEnemy(emee[0], "b");
                        }
                    } else if (emee[0].name == "monster") {
                        s.count += 1;
                        if (emee[0].scaleX == 1) {
                            shootPeople.MonsterManager.recoveryEnemy(emee[0], "m");
                        } else {
                            shootPeople.MonsterManager.recoveryEnemy(emee[0], "b");
                        }
                    }
                    // s.removeChild(e.target.parent);
                    s.goldcoin.fraction_txt.text = s.count;

                }
            }
            s.arrowList = s.arrowList.concat(cArrowList);
            s.timeCount++;
            s.monsterTime++;
            if (s.timeCount >= 30) {
                s.timeCount = 0;
                var monster = shootPeople.MonsterManager.getEnemy();
                if (Math.random() > 0.5) {
                    monster.motion = "x";
                    monster.x = 700;
                } else {
                    monster.motion = "y";
                    monster.x = -100;
                }
                monster.visible = true;
                monster.speed = Math.random() * 5 + 4;
                monster.y = Math.random() * 500 + 100;
                s.addChild(monster);
                s.enemyList.push(monster);
            }
            if (s.monsterTime >= 300) {
                s.monsterTime = 0;
                var smallEnemyDevil = shootPeople.DevilManager.getEnemy();
                smallEnemyDevil.speed = Math.random() * 5 + 4;
                if (Math.random() > 0.5) {
                    smallEnemyDevil.motion = "x";
                    smallEnemyDevil.x = 700;
                } else {
                    smallEnemyDevil.motion = "y";
                    // enemy.scores = -1;
                    smallEnemyDevil.x = -100
                }
                smallEnemyDevil.visible = true;
                smallEnemyDevil.y = Math.random() * 500 + 100;
                s.addChild(smallEnemyDevil);
                s.enemyList.push(smallEnemyDevil);
            }
            if (s.arrowTime < 8) {
                s.arrowTime++;
            }
        }
        s.gongRotationHandler()
    });

    var timer = new annie.Timer(500);
    timer.addEventListener(annie.Event.TIMER, function (e) {
        s.skillCoolingHandler(s.arrowType);
    });
    timer.start()
};
A2xExtend(shootPeople.Game, annie.Sprite);
//技能选中
shootPeople.Game.prototype.ArrowSkill = function (e) {
    var s = this;
    for (var i = 1; i <= 3; i++) {
        if ((e.charAt(e.length - 1)) == i) {
            if (!s["skillStatus" + i]) {
                s["skill" + i].gotoAndStop(2);
                s.arrowType = i;
                s["skillCoolingTime" + i] = s.skillCoolingArr[i - 1];
            }
        }
    }
    s.launchArrow();
};
shootPeople.Game.prototype.gongRotationHandler = function (y, x) {
    var s = this;
    if (!s.rotaionFalse) {
        s.zzl -= 4;
    } else {
        s.zzl += 4;
    }

    s.people.gong.rotation = s.zzl;
    var rotationAbs = Math.abs(s.people.gong.rotation);
    if (rotationAbs > 150) {
        s.rotaionFalse = true;
    } else if (rotationAbs <= 0) {
        s.rotaionFalse = false;
    }
    s.gongRotation = s.zzl;


};
//添加箭矢
shootPeople.Game.prototype.addArrowHandler = function (pointRadian, rotation, type) {
    var s = this;
    if (type > 1) {
        s["skillStatus" + (type - 1)] = true;
    }
    // console.log(s.arrowType)
    var bullet = shootPeople.ArrowManager.getArrow();
    bullet.direction = pointRadian;
    bullet.x = s.people.x + s.people.gong.x;
    bullet.y = s.people.y + s.people.gong.y;
    bullet.startX = s.people.x + s.people.gong.x;
    bullet.startY = s.people.y + s.people.gong.y;
    bullet.rotation = rotation;
    bullet.speed = 25;
    bullet.dis = 0;
    bullet.type = s.arrowType;
    s.addChild(bullet);
    s.arrowList.push(bullet);
    s.people.gong.gotoAndPlay(2);
    if (type == 3) {
        type = 1;
    }
    bullet.gotoAndStop(type);
};
//游戏结束
shootPeople.Game.prototype.gameOver = function () {
    var s = this;
    s.isStart = false;
    userInfo.score = s.count;
    annie.globalDispatcher.dispatchEvent("onJump", "shootPeople.Ranking");
};


//发射箭矢
shootPeople.Game.prototype.launchArrow = function () {
    var s = this;
    if (s.arrowTime == 8) {
        s.arrowTime = 0;
        if (s.arrowType == 2) {
            s.skill2.m.gotoAndPlay(2);
            s.billionSheSound.play();
            for (var i = 0; i < 7; i++) {
                s.addArrowHandler((s.gongRotation + s.arrowAngleArr[i]) * Math.PI / 180, s.gongRotation + s.arrowAngleArr[i], 3)
            }
        } else if (s.arrowType == 1) {
            s.sheSound.play();
            s.skill1.m.gotoAndPlay(2);
            s.addArrowHandler(s.gongRotation * Math.PI / 180, s.gongRotation, s.arrowType + 1);
        } else if (s.arrowType == 3) {
            s.skill3.m.gotoAndPlay(2);
            s.sheSound.play();
            s.addArrowHandler(s.gongRotation * Math.PI / 180, s.gongRotation, s.arrowType + 1);
        } else {
        }


        s.arrowType = 0;
    }
};
//技能冷却
shootPeople.Game.prototype.skillCoolingHandler = function (type) {
    var s = this;
    if (s.skillStatus1) {
        if (s.skillCoolingTime1 > 0) {
            s.skillCoolingTime1--;
            s.skill1.cooling_txt.text = s.skillCoolingTime1;
        } else {
            s.skillStatus1 = false;
            s.skill1.cooling_txt.visible = false;

        }
    }
    if (s.skillStatus2) {
        if (s.skillCoolingTime2 > 0) {
            s.skillCoolingTime2--;
            s.skill2.cooling_txt.text = s.skillCoolingTime2;
        } else {
            s.skillStatus2 = false;
            s.skill2.cooling_txt.visible = false;
        }
    }
    if (s.skillStatus3) {
        if (s.skillCoolingTime3 > 0) {
            s.skillCoolingTime3--;
            s.skill3.cooling_txt.text = s.skillCoolingTime3;
        } else {
            s.skillStatus3 = false;
            s.skill3.cooling_txt.visible = false;
        }
    }
}
;
